<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="商品：">
          <el-select v-model="listPram.goodsId" placeholder="商品" clearable style="width: 100%">
            <el-option
              v-for="(item,index) in goodsList.list"
              :key="index"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户关键词：">
          <el-input v-model="listPram.keyword" placeholder="请输入姓名或手机号" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label="村组" prop="village" v-if="identity===1">
          <el-cascader  class="selWidth"
                       v-model="listPram.villageId"
                       :options="villageList"
                       :props="optionProps"
                       @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list"  v-loading="loading" size="mini" :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column
          prop="id"
          label="ID"
          width="50"
        />
        <el-table-column label="用户" min-width="120">
          <template slot-scope="scope">
            <span>{{scope.row.userName}} | {{scope.row.phoneNumber}}</span>
          </template>
        </el-table-column>
        <el-table-column label="商品" min-width="150">
          <template slot-scope="scope">
            <span>{{scope.row.goodsName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="兑换数量" min-width="80">
          <template slot-scope="scope">
            <span>{{scope.row.num}}</span>
          </template>
        </el-table-column>
        <el-table-column label="花费积分" prop="integral" min-width="80"/>
        <!-- <el-table-column
          label="头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px!important; height: 36px!important;"
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
              />
            </div>
          </template>
        </el-table-column> -->
        <el-table-column label="村级" prop="villageText" min-width="80"/>
        <el-table-column label="状态" prop="statusText" min-width="80">
          <template slot-scope="scope">
            <span :style="'color:'+handleGetLightColor(scope.row.status)">{{scope.row.statusText}}</span>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" prop="createTime" min-width="80"/>
        <el-table-column label="核销时间" min-width="80">
          <template slot-scope="scope">
            <span>{{scope.row.exchangeTime | filterEmpty}}</span>
          </template>
        </el-table-column>
<!--        <el-table-column label="排序" prop="sort" min-width="120" />-->
        <el-table-column label="操作" min-width="130" align="center" fixed="right">
          <template slot-scope="scope">
            <div v-if="[1,2].indexOf(scope.row.status) === -1">
              <el-button icon="el-icon-success" type="primary" size="mini" @click="handlerOpenEdit(scope.row)"  v-hasPermi="['admin:goods:exchange:check']">核销</el-button>
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="listPram.page"
        :page-sizes="constants.page.limit"
        :layout="constants.page.layout"
        :total="listData.count"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!--审核-->
    <el-dialog
      :visible.sync="editDialogConfig.visible"
      title="商品核销"
      destroy-on-close
      :close-on-click-modal="false"
      width="700px"
    >
      <edit
        v-if="editDialogConfig.visible"
        :edit-data="editDialogConfig.editData"
        @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import * as goodsApi from '@/api/goods.js'
import * as roleApi from '@/api/role.js'
import { checkPermi } from "@/utils/permission";
import * as selfUtil from "@/utils/ZBKJIutil";
import * as categoryApi from "@/api/categoryApi";
import { getLightColor } from "@/utils";
import edit from './edit'
import { selectTreeApi } from '@/api/systemadmin'
export default {
  // name: "index"
  components: {edit },
  data() {
    return {
      constants:this.$constants,
      loading:false,
      listData: { list: [] },
      listPram: {
        keyword: '',
        goodsId:null,
        /*departmentId: null,
        ipAddress: null,
        macAddress: null,
        personId: null,*/
        page: 1,
        limit: this.$constants.page.limit[0],
        villageId:[]
      },
      editDialogConfig: {
        visible: false,
        editData: {}
      },

      goodsList:{},
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children',
      },
      villageList:[],
    }
  },
  mounted() {
    this.handleGetGoodsExchangeList()

    this.handleGetGoodsList() //用于下拉
    this.selectTreeList()
  },
  computed: {
    identity() {
      return this.$store.getters.id
    }
  },
  methods: {
    handleChange(value) {
      this.listPram.villageId=value
    },
    selectTreeList() {
      selectTreeApi().then(data => {
        this.villageList = data
      })
    },
    checkPermi,
    handleGetGoodsList() {
      const _pram = {
        page: 1,
        limit: 10000
      }
      goodsApi.getList( _pram ).then(data => {
        this.goodsList = data
      })
    },
    handleGetLightColor(status){
      return getLightColor(status)
    },

    handleSearch() {
      this.listPram.page = 1
      this.handleGetGoodsExchangeList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.handleGetGoodsExchangeList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.handleGetGoodsExchangeList()
    },
    /*handlerOpenDel(rowData) {
      this.$confirm('确认删除当前数据').then(() => {
        const _pram = { id: rowData.id}
        activityApi.doDel(_pram).then(data => {
          this.$message.success('删除数据成功')
          this.handleGetGoodsExchangeList()
        })
      })
    },*/

    handleGetGoodsExchangeList() {
      let listPram=JSON.parse(JSON.stringify(this.listPram));
      listPram.villageId=listPram.villageId[2]
      goodsApi.getExchangeList( listPram ).then(data => {
        this.listData.count = data.count
        this.listData.list = data.list
      })
    },
    handlerOpenEdit(editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.visible = true
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.handleGetGoodsExchangeList()
    },
    resetQuery(){
      this.listPram.keyword = '';
      this.listPram.goodsId = null;
      this.listPram.villageId=[];
      this.handleGetGoodsExchangeList()
    }
  }
}
</script>

<style scoped>

</style>
